﻿using Exam.BLL;
using Exam.DBHelper;
using Exam.Model;
using ExaminationWeb.Common;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ExaminationWeb.Controllers
{
    public class ProvinceController : Controller
    {
        //
        // GET: /Province/
        ProvinceService provinceService = new ProvinceService();
        public ActionResult ProvinceList()
        {
            return View();
        }
        public ActionResult ProvinceAdd()
        {
            return View();
        }
        public ActionResult ProvinceUpdate(string id)
        {
            Province province = provinceService.LoadEntites(e => e.ProvinceID == Guid.Parse(id)).First();
            ViewData["Province"] = province;
            return View();
        }
        #region 页面事件
        public JsonResult GetAllInfos()
        {
            int pageIndex = Request["sEcho"] == null ? 1 : int.Parse(Request["sEcho"]);
            int pageSize = Request["iDisplayLength"] == null ? 15 : int.Parse(Request["iDisplayLength"]);
            int pageStart = Request["iDisplayStart"] == null ? 0 : int.Parse(Request["iDisplayStart"]);

            string SortDirindex = Request["iSortCol_0"] == null ? "" : Request["iSortCol_0"];
            string SortDir = "";
            string DataProp = "";
            if (!string.IsNullOrEmpty(SortDirindex))
            {
                DataProp = Request["mDataProp_" + SortDirindex] == null ? "" : Request["mDataProp_" + SortDirindex];
                SortDir = Request["sSortDir_0"] == null ? "" : Request["sSortDir_0"]; //asc or desc
            }
            int totalcount = 0;
            string provinceName = Request["provinceName"] == null ? "" : Request["provinceName"];

            string isdelete = Request["isactive"] == null ? "-1" : Request["isactive"];

            IQueryable<Province> data;
            if (isdelete == "-1")
            {
                if (string.IsNullOrEmpty(provinceName))
                {
                    data = provinceService.LoadEntites(e => true, DataProp, SortDir == "asc", pageStart, pageSize, out totalcount);
                }
                else
                {
                    data = provinceService.LoadEntites(e => e.ProvinceName.ToLower().Contains(provinceName.ToLower()), DataProp, SortDir == "asc", pageStart, pageSize, out totalcount);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(provinceName))
                {
                    data = provinceService.LoadEntites(e => e.IsActive == int.Parse(isdelete), DataProp, SortDir == "asc", pageStart, pageSize, out totalcount);
                }
                else
                {
                    data = provinceService.LoadEntites(e => e.IsActive == int.Parse(isdelete) && e.ProvinceName.ToLower().Contains(provinceName.ToLower()), DataProp, SortDir == "asc", pageStart, pageSize, out totalcount);
                }
            }
            var result = new { iTotalDisplayRecords = totalcount, iTotalRecords = totalcount, aaData = data, sEcho = 0 };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public string AddInfo(Province province)
        {
            try
            {
                if (IsExistData(province).ProvinceID.ToString() == "00000000-0000-0000-0000-000000000000")
                {
                    province.ProvinceID = Guid.NewGuid();
                    DateTime time = DateTime.Now;
                    province.CreateTime = time;
                    province.CreateUserID = UserAccount.UserID;
                    province.UpdateTime = time;
                    Province newProvince = provinceService.AddEntity(province);

                    return newProvince.ProvinceID.ToString();
                }
                else
                {
                    return "repeat";
                }

            }
            catch (Exception e)
            {
                return "";
            }
        }
        [HttpPost]
        public string UpdateInfo(Province province)
        {
            try
            {
                Province _pro = IsExistData(province);
                if (_pro.ProvinceID.ToString() == "00000000-0000-0000-0000-000000000000" || _pro.ProvinceID == province.ProvinceID)
                {
                    province.UpdateTime = DateTime.Now;
                    province.UpdateUserID = UserAccount.UserID;
                    provinceService.UpdateEntity(province);
                    return "suc";
                }
                else
                {
                    return "repeat";
                }
            }
            catch (Exception e)
            {
                return "err";
            }
        }
        #endregion
        #region 公用
        public Province IsExistData(Province province)
        {
            SQLHelper commonservice = new SQLHelper();
            Dictionary<string, SqlParameter> dic = new Dictionary<string, SqlParameter>();
           
                SqlParameter parname = new SqlParameter("@ProvinceName", System.Data.SqlDbType.NVarChar);
                parname.Value = province.ProvinceName;
                dic.Add("ProvinceName", parname);
            return commonservice.getExistData<Province>("Province", dic);
        }

        #endregion
    }
}
